<template>
  <div class="commentArea">
    <div class="active" v-if="isShowTextarea">
      <textarea
        @blur="hideTextarea"
        v-model="commentValue"
        ref="textarea"
        :placeholder="placeholderText"
      ></textarea>

      <div class="btnSend" @click="publish">发表</div>
    </div>

    <div class="inactive" v-if="!isShowTextarea">
      <input 
        type="text" 
        placeholder="发表评论领积分..." 
        @focus="showTextarea" 
        v-model="commentValue" 
      />

      <div class="pinlunBox">
        <div class="iconfont iconpinglun"></div>
        <div class="pinlunNum">{{ postDetails.comment_length }}</div>
      </div>

      <div class="iconfont iconshoucang"></div>
      <div class="iconfont iconfenxiang"></div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShowTextarea: false,
      commentValue: ""
    };
  },

  props: ["parentInfo","postDetails"],

  computed: {
    placeholderText(){
      if(this.parentInfo.nickname){
        return "回复@" + this.parentInfo.nickname
      }else{
        return "友善评论、理性发言、阳光心灵";
      }
    }
  },

  methods: {
    showTextarea() {
      this.isShowTextarea = true;

      // 会报错，原因：vue是根据生命周期循环
      // vue会实现全部代码执行，才更新页面
      // 这里无法获取到textarea标签
      // 使用定时器是可以的，但时间无法估计
      // this.$refs.textarea.focus();

      // vue提供了this.$nextTick()这个函数来对dom进行异步更新
      this.$nextTick(() => {
        this.$refs.textarea.focus();
      });
    },

    hideTextarea() {
      setTimeout(() => {
        this.isShowTextarea = false;
      }, 100);
    },

    publish() {

      let data = {
        content: this.commentValue
      };

      if (this.parentInfo) {
        data.parent_id = this.parentInfo.id;
      }

      this.$axios({
        url: "/post_comment/" + this.$route.params.id,
        method: "POST",
        data
      }).then(res => {
        console.log("发布评论返回结果", res);
        const { message } = res.data;

        if (message === "评论发布成功") {
          this.$toast.success(message);

          this.$emit('reloadComment')

          this.commentValue = "";
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.commentArea {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  padding: 2.778vw 5.556vw;
  box-sizing: border-box;
  background-color: #fff;
  border-top: 0.278vw solid #f3f3f3;

  .active {
    display: flex;
    align-items: flex-end;

    textarea {
      flex: 1;
      background-color: #f3f3f3;
      border: 0;
      outline: 0;
      border-radius: 2.222vw;
      height: 16.667vw;
      text-indent: 1.111vw;
      padding: 2.222vw;
      box-sizing: border-box;
      resize: none;
      margin-right: 2.778vw;
    }

    textarea::-webkit-input-placeholder {
      font-size: 3.889vw;
    }

    .btnSend {
      width: 13.889vw;
      height: 7.222vw;
      line-height: 7.222vw;
      text-align: center;
      color: #fff;
      border-radius: 3.611vw;
      background-color: #fd4a68;
      font-size: 3.333vw;
    }
  }

  .inactive {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;

    input {
      height: 9.444vw;
      line-height: 9.444vw;
      border: none;
      outline: none;
      border-radius: 4.722vw;
      background-color: #f3f3f3;
      text-indent: 4.444vw;
      color: #333;
    }

    input::-webkit-input-placeholder {
      font-size: 3.889vw;
    }

    .iconfont {
      font-size: 7.222vw;
      color: #575757;
    }

    .pinlunBox {
      position: relative;

      .pinlunNum {
        position: absolute;
        width: 8.333vw;
        height: 4.444vw;
        line-height: 4.444vw;
        background-color: #fd4a68;
        border-radius: 2.222vw;
        color: #fff;
        font-size: 3.333vw;
        text-align: center;
        top: -0.556vw;
        left: 2.778vw;
      }
    }
  }
}
</style>